package com.yanfan.energy.util;


import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.Map;


public class NumberUtils {


    private static final String MILLION_UNIT = "万";

    private static final String TEN_MILLION_UNIT = "千万";

    private static final String BILLION_UNIT = "亿";


    /**
     * 1万
     */
    private static final Double TEN_THOUSAND = 10000.0;

    /**
     * 1千万
     */
    private static final Double TEN_MILLION = 10000000.0;


    /**
     * 一亿
     */
    private static final Double ONE_HUNDRED_MILLION = 100000000.0;


    /**
     * 将数字转换成以万为单位或者以亿为单位，因为在前端数字太大显示有问题
     *
     * @param amount 报销金额
     * @return
     * @author
     * @version 1.00.00
     * @date 2018年1月18日
     */
    public static String amountConv(double amount) {
        //最终返回的结果值
        String result = String.valueOf(amount);
        //四舍五入后的值
        double value = 0;
        //转换后的值
        double tempValue = 0;
        //余数
        double remainder = 0;

        if (amount >= TEN_THOUSAND && amount < TEN_MILLION) {
            tempValue = amount / TEN_THOUSAND;
            remainder = amount % TEN_THOUSAND;
            //余数小于5000则不进行四舍五入
            if (remainder < (TEN_THOUSAND / 2)) {
                value = formatNumber(tempValue, false);
            } else {
                value = formatNumber(tempValue, true);
            }
            //如果值刚好是10000万，则要变成1亿
            if (value == TEN_THOUSAND) {
                result = value / TEN_THOUSAND + MILLION_UNIT;
            } else {
                result = value + MILLION_UNIT;
            }
        }
        //金额大于1千万小于1亿
        else if (amount >= TEN_MILLION && amount < ONE_HUNDRED_MILLION) {
            tempValue = amount / TEN_MILLION;
            remainder = amount % TEN_MILLION;

            //余数小于5000则不进行四舍五入
            if (remainder < (TEN_MILLION / 2)) {
                value = formatNumber(tempValue, false);
            } else {
                value = formatNumber(tempValue, true);
            }
            //如果值刚好是10000万，则要变成1亿
            if (value == TEN_MILLION) {
                result = value / TEN_MILLION + TEN_MILLION_UNIT;
            } else {
                result = value + TEN_MILLION_UNIT;
            }
        }
        //金额大于1亿
        else if (amount >= ONE_HUNDRED_MILLION) {
            tempValue = amount / ONE_HUNDRED_MILLION;
            remainder = amount % ONE_HUNDRED_MILLION;
            //余数小于50000000则不进行四舍五入
            if (remainder < (ONE_HUNDRED_MILLION / 2)) {
                value = formatNumber(tempValue, false);
            } else {
                value = formatNumber(tempValue, true);
            }
            result = value + BILLION_UNIT;
        } else {
            result = String.valueOf(formatNumber(amount, true));
        }

        return result;
    }

    /**
     * 将数字转换成以万为单位或者以亿为单位，因为在前端数字太大显示有问题
     * 返回 数字 和单位
     *
     * @param amount 报销金额
     * @return
     * @author
     * @version 1.00.00
     * @date 2018年1月18日
     */
    public static Map<String, Object> amountConversion(Double amount) {
        if (amount == null) {
            return null;
        }
        String unitsName = null;
        //最终返回的结果值
        Map<String, Object> result = new HashMap<>();
        //四舍五入后的值
        String value = "0";
        //转换后的值
        double tempValue = 0;
        //余数
        double remainder = 0;

        if (amount >= TEN_THOUSAND && amount < TEN_MILLION) {
            tempValue = amount / TEN_THOUSAND;
            remainder = amount % TEN_THOUSAND;
            //余数小于5000则不进行四舍五入
            if (remainder < (TEN_THOUSAND / 2)) {
                value = formatNumberStr(tempValue, false);
            } else {
                value = formatNumberStr(tempValue, true);
            }
            //如果值刚好是10000万，则要变成1亿
            if (Double.valueOf(value) == TEN_THOUSAND) {
                result.put(NumberConstant.NUMBER, Double.valueOf(value) / TEN_THOUSAND);
            } else {
                result.put(NumberConstant.NUMBER, value);
            }
            unitsName = MILLION_UNIT;

        }
        //金额大于1千万小于1亿
        else if (amount >= TEN_MILLION && amount < ONE_HUNDRED_MILLION) {
            tempValue = amount / TEN_MILLION;
            remainder = amount % TEN_MILLION;

            //余数小于5000则不进行四舍五入
            if (remainder < (TEN_MILLION / 2)) {
                value = formatNumberStr(tempValue, false);
            } else {
                value = formatNumberStr(tempValue, true);
            }
            //如果值刚好是10000万，则要变成1亿
            if (Double.valueOf(value) == TEN_MILLION) {
                result.put(NumberConstant.NUMBER, Double.valueOf(value) / TEN_MILLION);
            } else {
                result.put(NumberConstant.NUMBER, value);
            }
            unitsName = TEN_MILLION_UNIT;
        }
        //金额大于1亿
        else if (amount >= ONE_HUNDRED_MILLION) {
            tempValue = amount / ONE_HUNDRED_MILLION;
            remainder = amount % ONE_HUNDRED_MILLION;
            //余数小于50000000则不进行四舍五入
            if (remainder < (ONE_HUNDRED_MILLION / 2)) {
                value = formatNumberStr(tempValue, false);
            } else {
                value = formatNumberStr(tempValue, true);
            }
            result.put(NumberConstant.NUMBER, value);
            unitsName = BILLION_UNIT;

        } else {
            result.put(NumberConstant.NUMBER, formatNumberStr(amount, true));
        }
        // 单位
        result.put(NumberConstant.NUMBER_UNITS, unitsName);
        return result;
    }

    /**
     * 对数字进行四舍五入，保留2位小数
     *
     * @param number   要四舍五入的数字
     * @param rounding 是否四舍五入
     * @return
     * @author
     * @version 1.00.00
     */
    public static String formatNumberStr(double number, boolean rounding) {
        int decimal = 0;
        BigDecimal bigDecimal = new BigDecimal(number);
        //  保留的小数点数
        String s = Double.toString(number);
        int length = s.substring(0, s.indexOf(".")).length();
        if (length < 4) {
            decimal = 4 - length;
        }
        if (rounding) {
            return bigDecimal.setScale(decimal, RoundingMode.HALF_UP).toPlainString();
        } else {
            return bigDecimal.setScale(decimal, RoundingMode.DOWN).toPlainString();
        }
    }

    /**
     * 对数字进行四舍五入，保留2位小数
     *
     * @param number   要四舍五入的数字
     * @param rounding 是否四舍五入
     * @return
     * @author
     * @version 1.00.00
     */
    public static Double formatNumber(double number, boolean rounding) {
        int decimal = 0;
        BigDecimal bigDecimal = new BigDecimal(number);
        //  保留的小数点数
        String s = Double.toString(number);
        int length = s.substring(0, s.indexOf(".")).length();
        if (length < 4) {
            decimal = 4 - length;
        }
        if (rounding) {
            return bigDecimal.setScale(decimal, RoundingMode.HALF_UP).doubleValue();
        } else {
            return bigDecimal.setScale(decimal, RoundingMode.DOWN).doubleValue();
        }
    }

    /**
     * 根据值返回值
     *
     * @param amount
     * @return java.lang.Double
     * @author caijinkang
     * @date 2022/12/18 9:47
     */
    public static Double amountConvGetNumber(double amount) {
        //最终返回的结果值
        Double result = amount;
        //四舍五入后的值
        double value = 0;
        //转换后的值
        double tempValue = 0;
        //余数
        double remainder = 0;

        if (amount >= TEN_THOUSAND && amount < TEN_MILLION) {
            tempValue = amount / TEN_THOUSAND;
            remainder = amount % TEN_THOUSAND;
            //余数小于5000则不进行四舍五入
            if (remainder < (TEN_THOUSAND / 2)) {
                value = formatNumber(tempValue, false);
            } else {
                value = formatNumber(tempValue, true);
            }
            //如果值刚好是10000万，则要变成1亿
            if (value == TEN_THOUSAND) {
                result = value / TEN_THOUSAND;
            } else {
                result = value;
            }
        }
        //金额大于1千万小于1亿
        else if (amount >= TEN_MILLION && amount < ONE_HUNDRED_MILLION) {
            tempValue = amount / TEN_MILLION;
            remainder = amount % TEN_MILLION;

            //余数小于5000则不进行四舍五入
            if (remainder < (TEN_MILLION / 2)) {
                value = formatNumber(tempValue, false);
            } else {
                value = formatNumber(tempValue, true);
            }
            //如果值刚好是10000万，则要变成1亿
            if (value == TEN_MILLION) {
                result = value / TEN_MILLION;
            } else {
                result = value;
            }
        }
        //金额大于1亿
        else if (amount >= ONE_HUNDRED_MILLION) {
            tempValue = amount / ONE_HUNDRED_MILLION;
            remainder = amount % ONE_HUNDRED_MILLION;
            //余数小于50000000则不进行四舍五入
            if (remainder < (ONE_HUNDRED_MILLION / 2)) {
                value = formatNumber(tempValue, false);
            } else {
                value = formatNumber(tempValue, true);
            }
            result = value;
        } else {
            result = formatNumber(amount, true);
        }
        return result;
    }

    /**
     * 根据值返回单位
     *
     * @param amount
     * @return java.lang.String
     * @author caijinkang
     * @date 2022/12/18 9:47
     */
    public static String amountConvGetUnit(double amount) {
        //最终返回的结果值
        String result = String.valueOf(amount);
        //四舍五入后的值
        double value = 0;
        //转换后的值
        double tempValue = 0;
        //余数
        double remainder = 0;

        if (amount >= TEN_THOUSAND && amount < TEN_MILLION) {
            tempValue = amount / TEN_THOUSAND;
            remainder = amount % TEN_THOUSAND;
            //余数小于5000则不进行四舍五入
            if (remainder < (TEN_THOUSAND / 2)) {
                value = formatNumber(tempValue, false);
            } else {
                value = formatNumber(tempValue, true);
            }
            //如果值刚好是10000万，则要变成1亿
            if (value == TEN_THOUSAND) {
                result = MILLION_UNIT;
            } else {
                result = MILLION_UNIT;
            }
        }
        //金额大于1千万小于1亿
        else if (amount >= TEN_MILLION && amount < ONE_HUNDRED_MILLION) {
            tempValue = amount / TEN_MILLION;
            remainder = amount % TEN_MILLION;

            //余数小于5000则不进行四舍五入
            if (remainder < (TEN_MILLION / 2)) {
                value = formatNumber(tempValue, false);
            } else {
                value = formatNumber(tempValue, true);
            }
            //如果值刚好是10000万，则要变成1亿
            if (value == TEN_MILLION) {
                result = TEN_MILLION_UNIT;
            } else {
                result = TEN_MILLION_UNIT;
            }
        }
        //金额大于1亿
        else if (amount >= ONE_HUNDRED_MILLION) {
            tempValue = amount / ONE_HUNDRED_MILLION;
            remainder = amount % ONE_HUNDRED_MILLION;
            //余数小于50000000则不进行四舍五入
            if (remainder < (ONE_HUNDRED_MILLION / 2)) {
                value = formatNumber(tempValue, false);
            } else {
                value = formatNumber(tempValue, true);
            }
            result = BILLION_UNIT;
        } else {
            result = String.valueOf(formatNumber(amount, true));
        }

        return result;
    }

}
